<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class CollageJoin extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_claim_collage_join';


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:33
     * @功能说明:
     */
    public function adminDataList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_claim b','a.claim_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.title as claim_title')
                ->group('a.id')
                ->order('a.top desc,a.id')
                ->paginate($page)
                ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-16 15:25
     * @功能说明:用户拼团记录
     */
    public function userCollageLimit($dis,$limit=3){

        $data = $this->alias('a')
                ->join('lbfarm_claim b','a.claim_id = b.id','left')
                ->join('lbfarm_v2_claim_collage_start c','a.start_id = c.id','left')
                ->where($dis)
                ->field('a.*,b.title as claim_title,b.cover as claim_cover,c.end_time,c.success_num,c.have_order_num as have_num')
                ->group('a.id')
                ->order('a.id desc')
                ->limit($limit)
                ->select()
                ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['surplus_num'] = ($v['success_num'] - $v['have_num'])>0?$v['success_num'] - $v['have_num']:0;
            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-18 14:50
     * @功能说明:用户参与拼团的次数
     */
    public function userCollageCount($user_id){

        $dis[] = ['a.user_id','=',$user_id];

        $dis[] = ['a.status','>=',1];

        $data = $this->alias('a')
            ->join('lbfarm_claim b','a.claim_id = b.id','left')
            ->join('lbfarm_v2_claim_collage_start c','a.start_id = c.id','left')
            ->where($dis)
            ->field('a.*,b.title as claim_title,b.cover as claim_cover,c.end_time,c.success_num')
            ->group('a.id')
            ->count();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-16 15:25
     * @功能说明:用户拼团记录
     */
    public function userCollagePage($dis,$limit=10){

        $data = $this->alias('a')
            ->join('lbfarm_claim b','a.claim_id = b.id','left')
            ->join('lbfarm_v2_claim_collage_start c','a.start_id = c.id','left')
            ->where($dis)
            ->field('a.*,b.title as claim_title,b.cover as claim_cover,c.end_time,c.success_num')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($limit)
            ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['user_avatar'] = $this->collageUserAvatar($v['start_id'],4);

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-16 18:06
     * @功能说明:拼团用户头像
     */
    public function collageUserAvatar($id,$limit=5){

        $dis[] = ['a.start_id','=',$id];

        $dis[] = ['a.status','>=',1];

        $data = $this->alias('a')
                ->join('lbfarm_user_list b','a.user_id = b.id','left')
                ->where($dis)
                ->limit($limit)
                ->column('b.avatarUrl');

        return array_values($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-08-17 16:25
     * @功能说明：参与拼团
     */
    public function joinCollage($order,$data,$is_start){

        $collage_model = new ClaimCollage();

        if(empty($data)||$data['end_time']<time()){

            return ['code'=>500,'msg'=>'拼团已经结束'];

        }

        $collage = $collage_model->dataInfo(['id'=>$data['atv_id']]);

        if(empty($collage)){

            return ['code'=>500,'msg'=>'拼团已经结束'];

        }

        $dis = [

            'user_id' => $order['user_id'],

            'atv_id'  => $data['atv_id'],

            'is_start'=> 0
        ];
        //校验参与次数
        $count = $this->where($dis)->where('status','>',-1)->count();

        if($collage['join_times']<=$count){

            return ['code'=>500,'msg'=>'拼团超过参与次数'];
        }

        $dis = [

            'start_id' => $data['id']
        ];

        if($data['have_num']>=$data['success_num']){

            return ['code'=>500,'msg'=>'超过拼团人数'];

        }
        //校验库存
        $num = $this->where($dis)->where('status','>',-1)->sum('num');

        if($num>=$collage['stock']){

            return ['code'=>500,'msg'=>'超过活动库存'];

        }

        $dis['user_id'] = $order['user_id'];

        $find = $this->where($dis)->where('status','>',-1)->find();

        if(!empty($find)){

            return ['code'=>500,'msg'=>'你已经参加过改拼团了'];
        }

        $insert = [

            'uniacid' => $order['uniacid'],

            'user_id' => $order['user_id'],

            'claim_id'=> $order['claim_id'],

            'price'   => $collage['price'],

            'status'  => 0,

            'order_id'=> $order['id'],

            'num'     => $order['num'],

            'atv_id'  => $data['atv_id'],

            'start_id'=> $data['id'],

            'is_start'=> $is_start,

        ];

        $res = $this->dataAdd($insert);

        $start_model = new CollageStart();

        $start_model->where(['id'=>$data['id']])->update(['have_order_num'=>Db::Raw('have_order_num+1')]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-18 17:23
     * @功能说明:用户是否还能参加该拼团
     */
    public function collageCanJoin($user_id,$start_id){

        $find = $this->where(['user_id'=>$user_id,'start_id'=>$start_id])->where('status','in',[0,1,2])->find();

        if(!empty($find)){

            return 0;
        }

        $collage_model = new ClaimCollage();

        $dis = [

            'b.id' => $start_id
        ];

        $data = $collage_model->alias('a')
                ->join('lbfarm_v2_claim_collage_start b','a.id = b.atv_id')
                ->where($dis)
                ->field('b.atv_id,a.join_times')
                ->find();

        if(!empty($data)){

            $data = $data->toArray();

            $count = $this->where(['user_id'=>$user_id,'atv_id'=>$data['atv_id']])->where('status','in',[0,1,2])->count();

            if($data['join_times']<=$count){

                return 0;
            }

        }

        return 1;

    }





}